﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;

namespace BingoGameServerConsole
{
    public class RoomManager
    {
        private static RoomManager instanse=null;
        private ConcurrentDictionary<long, Work> workDict = new ConcurrentDictionary<long, Work>();
        private ConcurrentDictionary<long, Room> roomDict = new ConcurrentDictionary<long, Room>();//所有房间列表  便于查询

        private List<Work> workList = new List<Work>();
        private long curWorkID = 0;//当前房间索引ID  递增 递增  原子安全

        public RoomManager()
        {
            
        }
        public static RoomManager Instanse
        {
            get
            {
                if (instanse == null)
                    instanse = new RoomManager();
                return instanse;
            }
        }

        /// <summary>
        /// 连接成功后会进行自动分配
        /// </summary>
        /// <param name="player"></param>
        public Room AutoAlloc(User player)
        {//自动分配
            Room room = null;
            lock (workList)
            {
                foreach (Work work in workList)
                {
                    room = work.AllocationRoom();
                    if (room != null)
                    {
                        player.ChangeWorkId(work.id);
                        player.ChangeRoomId(room.id);
                        break;
                    }
                }
            }
            if (room == null)
            {//遍历所有的工作线程 仍然没有找到房间，证明 房间都满了 需要启动新的work
                NewWork();
                return AutoAlloc(player);//再次分配
            }

            return room;

        }
        /// <summary>
        /// 启动work
        /// </summary>
        /// <param name="num">work 数量</param>
        public void StartWork(int num)
        {
            for (int i = 0; i < num; i++)
            {
                NewWork();
            }
        }
        private void NewWork()
        {
            
            long id = Interlocked.Increment(ref curWorkID);
            lock (workList)
            {
                Work work = new Work((int)id);
                workDict.TryAdd(id, work);
                workList.Add(work);
            }
        }

        public void AddRoom(int rid,Room room)
        {
            roomDict.TryAdd(rid, room);
        }
        public void DeleteRoom(int rid)
        {
            Room room = null;
            roomDict.TryRemove(rid,out room);
            
        }
        public Room GetRoom(long rid)
        {
            Room room = null;
            roomDict.TryGetValue(rid, out room);
            return room;
        }
        public void DeleteWork(int id)
        {
            Work work = null;
            workDict.TryRemove(id, out work);
            lock (workList)
            {
                workList.Remove(work);
            }
        }
        public void PushPackage(WorkObject obj)
        {
            long workid = ((User)obj.session.player).GetWorkId();
            Work work = null;
            workDict.TryGetValue(workid,out work);
            if(work !=null)
            {
                work.PushPackage(obj);
            }
            else
            {
                Console.WriteLine("RoomManager.PushPackage 找不到workid="+workid);
            }
        }
    }
}
